package com.lottery.api.impl;

import java.lang.reflect.Method;

import org.apache.thrift.TException;
import org.springframework.util.ReflectionUtils;

import com.lottery.api.IThriftEntryProcesser.Iface;
import com.lottery.util.FastJsonUtil;
import com.lottery.util.SpringUtil;

public class ThriftEntryProcesser implements Iface {

  @Override
  public final String entry(String serviceName, String methodName, String jsonParams) throws TException {
    Object serviceBean = SpringUtil.getApplicationContext().getBean(serviceName);
    Object res = null;
    if (jsonParams != null) {
      Method method = ReflectionUtils.findMethod(serviceBean.getClass(), methodName, String.class);
      res = ReflectionUtils.invokeMethod(method, serviceBean, jsonParams);
    } else {
      Method method = ReflectionUtils.findMethod(serviceBean.getClass(), methodName);
      res = ReflectionUtils.invokeMethod(method, serviceBean);
    }
    if (res == null) {
      return null;
    } else {
      return FastJsonUtil.bean2String(res);
    }
  }

}
